var app = getApp();
var bmap = require('../../libs/bmap-wx.js');
var wxMarkerData = []; 
var address;
Page({
  data: {
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    expertList: [{ //假数据
      img: "avatar.png",
      name: "欢顔",
      tag: "知名情感博主",
      answer: 134,
      listen: 2234,
      inputTxt: ''
    }],
    // address:[{
    //   name:'徐州爱琴海商务酒店',
    //   detail:'绿地商务城LOft3楼'
    // }],
    
    // 百度地图
    markers: [],
    latitude: '',
    longitude: '',
    placeData: {} 

  },
  makertap: function (e) {
    var that = this;
    var id = e.markerId;
    that.showSearchInfo(wxMarkerData, id);
    that.changeMarkerColor(wxMarkerData, id);
  }, 
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function () {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });

  // 百度地图
    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'bzQsfmU86UsaqMOeZzXFrjbkVAUKcVYW'
    });
    var fail = function (data) {

    };
    var success = function (data) {
      // 百度地图放置位置
      that.setData({
        address: data.wxMarkerData
      });

      wxMarkerData = data.wxMarkerData;
      that.setData({
        markers: wxMarkerData
      });
      that.setData({
        latitude: wxMarkerData[0].latitude
      });
      that.setData({
        longitude: wxMarkerData[0].longitude
      });

    }
    // 发起POI检索请求 
    BMap.search({
      "query": '小区',
      fail: fail,
      success: success,
      // 此处需要在相应路径放置图片文件 
      iconPath: '../../img/marker_red.png',
      // 此处需要在相应路径放置图片文件 
      iconTapPath: '../../img/marker_red.png'
    });
  },



  showSearchInfo: function (data, i) {
    var that = this;
    that.setData({
      placeData: {
        title: '名称：' + data[i].title + '\n',
        address: '地址：' + data[i].address + '\n',
        // telephone: '电话：' + data[i].telephone
      }
    });
  },

  formBindsubmit: function (mobile) {
    var transmit = mobile.detail.value;

    console.log(mobile.detail.value)

    var Smobile = mobile.detail.value.Dphone; //传入首页的手机号
    var Sname = transmit.Dsetname; //传入首页的用户名
    var Saddress_1 = transmit.Daddress1; //传入首页的地址1
    var Saddress_2 = transmit.Daddress2; //传入首页的详细地址

    if (Smobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (Smobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(Smobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    else{
      wx.navigateTo({
        url: '/pages/index/index?Smobile=' + Smobile + "&Sname=" + Sname + "&Saddress_1=" + Saddress_1 + "&Saddress_2=" + Saddress_2,
        // url: '/pages/index/index?mobile=' + mobile + "&name=" + name,
      })

    }
    return true;
    

    
  },

// 点击下方地址选择回填到上方
  getadd:function(e){
    let address = e.currentTarget.dataset.address;
    this.setData({
      inputTxt: address
    })
  },
  changeMarkerColor: function (data, i) {
    var that = this;
    var markers = [];
    for (var j = 0; j < data.length; j++) {
      if (j == i) {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_yellow.png";
      } else {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_red.png";
      }
      markers[j](data[j]);
    }
    that.setData({
      markers: markers
    }); 

  },
  skiped:function(){
    // wx.navigateTo({
    //   url: '/pages/index/index',
    // })
  },
  footerTap: app.footerTap
})